package com.mobimagic.lockscreen;

public class Charge {

    /**
     * 用service的最少剩余电量值
     */
    public static final float USE_SERVICE_REMIND_MIN_VALUE = 0.82f;

    @Override
    public String toString() {
        return "Charge [isCharging=" + isCharging + ", chargingValue=" + chargingValue + ", chargingValueType="
                + chargingValueType + ", chargingType=" + chargingType + ", startTime=" + startTime + ", fullTime="
                + fullTime + ", isFull=" + isFull + "]";
    }

    private static final int MAX_REMIND_TIME = 5 * 60 * 60;
    /**
     * 涓流充电时间最长为10分钟
     */
    public static final int TRICKLE_TIME = 10 * 60 * 1000;
    public static final float SPEED_MAX = 0.8f;
    public static final float CONTINUOUS_MAX = 1f;

    public static final float RED_MAX = 0.2f;
    public static final float YELLOW_MAX = 0.5f;

    private boolean isCharging;
    /**
     * 充电进度,电量百分比
     */
    private float chargingValue;
    private ChargingValueType chargingValueType;
    private boolean isRealValue = true;

    /**
    *
    */
    private ChargingType chargingType;

    /**
     * 开始充电的时间
     */
    private long startTime;

    /**
     * 充满电的时间
     */
    private long fullTime;

    private boolean isFull;

    private ChargingTheme chargingTheme;
    private ChargingType mLastChargeType;
    private long serviceRemaind;

    public ChargingTheme getChargingTheme() {
        return chargingTheme;
    }

    public void setChargingTheme(ChargingTheme chargingTheme) {
        this.chargingTheme = chargingTheme;
    }

    public ChargingType getChargingType() {
        return chargingType;
    }

    public void setChargingType(ChargingType chargingType) {
        this.chargingType = chargingType;
        if (chargingType != null) {
            mLastChargeType = chargingType;
        }
    }

    public ChargingValueType getChargingValueType() {
        return chargingValueType;
    }

    public void setChargingValueType(ChargingValueType chargingValueType) {
        this.chargingValueType = chargingValueType;
    }

    public boolean isFull() {
        return isFull;
    }

    public void setFull(boolean isFull) {

        this.isFull = isFull;
    }

    public boolean isRealValue() {
        return isRealValue;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public long getFullTime() {
        return fullTime;
    }

    public void setFullTime(long fullTime) {
        this.fullTime = fullTime;
    }

    public boolean isCharging() {
        return isCharging;
    }

    public void setCharging(boolean isCharging) {
        this.isCharging = isCharging;
    }

    public float getChargingValue() {
        return chargingValue;
    }

    public void setChargingValueAndCalc(float changingValue, boolean isReal) {
        this.chargingValue = changingValue;
        this.isRealValue = isReal;
        if (changingValue < SPEED_MAX) {
            chargingValueType = ChargingValueType.Speed;
        } else if (changingValue < 1) {
            chargingValueType = ChargingValueType.Continuous;
        } else {
            long current = System.currentTimeMillis();
            if ((current - fullTime) >= TRICKLE_TIME) {
                chargingValueType = ChargingValueType.TrickleComplete;
            } else if (current - fullTime < 0) {
                chargingValueType = ChargingValueType.Trickle;
                fullTime = current;
            } else {
                chargingValueType = ChargingValueType.Trickle;
            }
        }
        if (chargingValue < RED_MAX) {
            chargingTheme = ChargingTheme.Red;
        } else if (chargingValue < YELLOW_MAX) {
            chargingTheme = ChargingTheme.Yellow;
        } else {
            chargingTheme = ChargingTheme.Blue;
        }
    }

    /**
     * 获取充电剩余时间
     * 
     * @return 充电剩余分钟数
     */
    public long getChargingRemainingTime() {
        if (isRemaindFromBattery()) {
            return getRemaindTime();
        } else {
            return (serviceRemaind + 59) / (60);
        }
    }

    /**
     * 判断剩余时间是不是仅仅通过电量计算得来的
     */
    public boolean isRemaindFromBattery() {
        return chargingValue <= USE_SERVICE_REMIND_MIN_VALUE || serviceRemaind <= 0 || serviceRemaind > MAX_REMIND_TIME
                || chargingValueType == ChargingValueType.Trickle;
    }

    private long getRemaindTime() {
        int x = 0;
        if (chargingType == ChargingType.AC) {
            x = 130 * 60 * 1000;
        } else {
            x = 150 * 60 * 1000;
        }
        long remainTime = 0;
        if (chargingValueType != ChargingValueType.Trickle) {
            remainTime = (long) ((1 - chargingValue) * x);
        } else {
            remainTime = fullTime + 10 * 60 * 1000 - System.currentTimeMillis();
        }
        //大于1s按一分钟处理，小于1s按0分钟处理
        return (remainTime + (60 * 1000 - 1000)) / (60 * 1000);
    }

    /**
     * 电话，上网等功能时长(单位是小时)
     */
    public float getUseTimeForHour(UseTimeType useTimeType) {
        float result = useTimeType.getX(mLastChargeType) * chargingValue;
        return result;
    }

    public enum ChargingValueType {
        Speed, Continuous, Trickle, TrickleComplete;
    }

    public enum ChargingType {
        USB, AC;
    }

    public enum ChargingTheme {
        Red, Yellow, Blue;
    }

    public enum UseTimeType {
        Call(100 * 1000 / 60 / 400f, 100 * 500 / 60 / 400f), NetWork(100 * 1000 / 60 / 380f, 100 * 500 / 60 / 380f), Movie(
                100 * 1000 / 60 / 370f, 100 * 500 / 60 / 370f);
        private UseTimeType(float acX, float usbX) {
            this.acX = acX;
            this.usbX = usbX;
        }

        private float acX;
        private float usbX;

        public float getX(ChargingType chargingType) {
            if (chargingType == ChargingType.AC) {
                return acX * 1.3f;
            }
            return usbX * 1.5f;
        }
    }

    public void boost() {

    }

    public void setServiceRemaind(int remaind) {
        this.serviceRemaind = (long) remaind;

    }

}
